﻿using commonImportPlugin.Configs.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace commonImportPlugin.Configs.SsseFile
{
    public class SsseFileConfig : ConfigsBase,IcommonBase
    {
        //
        //    基本配置参数
        //
        #region
        /// <summary>
        /// 工厂配置
        /// </summary>
        public string factory { set; get; }
        /// <summary>
        /// 筛选数据文件规则
        /// (文件名正则[#]文件内容正则)
        /// </summary>
        public string fileFilterRules { set; get; }
        /// <summary>
        /// 编码格式
        /// </summary>
        public string encoding { set; get; }
        /// <summary>
        /// 社保数据的起始位置（1开始）
        /// </summary>
        public int dataIndex { set; get; }

        /// <summary>
        /// 社保数据的分隔符
        /// </summary>
        public string[] dataSeparators { set; get; }

        /// <summary>
        /// 社保银行卡号所处列
        /// </summary>
        public int[] subBankCardId { set; get; }
        /// <summary>
        /// 社保身份证号所处列
        /// </summary>
        public int[] subPersonId { set; get; }
        /// <summary>
        /// 社保卡号所处列
        /// </summary>
        public int[] subCardId { set; get; }
        /// <summary>
        /// 社保数据检查正则表达式
        /// </summary>
        public string dataCheckRegexs { set; get; }
        /// <summary>
        /// 该参数用于实现过滤之前已导入的数据
        /// 该参数值为：bankCardId,personId,ssCardId之一
        /// </summary>
        public string filterItem { set; get; }
        #endregion

        //
        //    其他
        //
        #region
        private static SsseFileConfig uniqueInstance;
        public void Init()
        {
            SsseFileConfig ssseFileConfig = new SsseFileConfig();
            string checkRegexs = @"^\d{1,}&(-\d{1,}|\d{1,})&(-\d{1,}|\d{1,})$";//检查参数配置的参数格式为：数字(+)&数字(-+)&数字(-+)

            //工厂配置
            ssseFileConfig.factory = configs["SsseFileFactory"];

            //获取文件扩展名
            if (!IsKeyExist("SsseFileFilterRules"))
                throw new Exception(SsseErrors.SsseFileFilterRulesError);

            ssseFileConfig.fileFilterRules = configs["SsseFileFilterRules"];

            //编码方式
            if (!IsKeyEmpty("SsseFileEncoding"))
                ssseFileConfig.encoding = configs["SsseFileEncoding"];
            else
                ssseFileConfig.encoding = "GB2312";//默认值

            //数据起始行（1开始)
            if (!IsKeyExist("SsseFileDataIndex"))
                throw new Exception(SsseErrors.SsseFileDataIndexError1);

            if (!Regex.IsMatch(configs["SsseFileDataIndex"], @"\d{1,}"))
                throw new Exception(SsseErrors.SsseFileDataIndexError2);

            ssseFileConfig.dataIndex = int.Parse(configs["SsseFileDataIndex"]);

            //社保数据分隔符
            if (!IsKeyExist("SsseFileDataSeparators"))
                throw new Exception(SsseErrors.SsseFileDataSeparatorsError);

            ssseFileConfig.dataSeparators = ToStringArrays(configs["SsseFileDataSeparators"]);

            //获取银行卡号
            if (!IsKeyEmpty("SsseFileSubBankCardId"))
            {
                if (!Regex.IsMatch(configs["SsseFileSubBankCardId"], checkRegexs))
                    throw new Exception(SsseErrors.SsseFileSubBankCardIdError);
                ssseFileConfig.subBankCardId = ToIntArrays(configs["SsseFileSubBankCardId"]);
            }

            //获取身份证号
            if (!IsKeyEmpty("SsseFileSubPersonId"))
            {
                if (!Regex.IsMatch(configs["SsseFileSubPersonId"], checkRegexs))
                    throw new Exception(SsseErrors.SsseFileSubPersonIdError);

                ssseFileConfig.subPersonId = ToIntArrays(configs["SsseFileSubPersonId"]);
            }

            //获取医保卡号
            if (!IsKeyEmpty("SsseFileSubCardId"))
            {
                if (!Regex.IsMatch(configs["SsseFileSubCardId"], checkRegexs))
                    throw new Exception(SsseErrors.SsseFileSubCardIdError);

                ssseFileConfig.subCardId = ToIntArrays(configs["SsseFileSubCardId"]);
            }

            //数据检查正则
            if (IsKeyExist("SsseFileDataCheckRegexs"))
            {
                ssseFileConfig.dataCheckRegexs = configs["SsseFileDataCheckRegexs"];
            }
            
            //过滤数据项（1开始）
            if (!IsKeyEmpty("SsseFileFilterItem"))
            {

                string[] items = new string[] { "bankCardId", "personId", "ssCardId" };
                string item = configs["SsseFileFilterItem"].Trim();

                if (!items.Contains(item))
                    throw new Exception(SsseErrors.SsseFileFilterItemError);

                ssseFileConfig.filterItem = configs["SsseFileFilterItem"];
            }

            uniqueInstance= ssseFileConfig;
        }
        /// <summary>
        /// 获取配置
        /// </summary>
        /// <returns></returns>
        public static SsseFileConfig GetConfig()
        {
            if (uniqueInstance == null)
            {
                return null;
            }
            return uniqueInstance;
        }
        #endregion
    } 
}
